package kaizone.haining.chaobohui.setup;

import java.net.URL;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.util.Utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;

public class WeiXin implements IWXAPIEventHandler {

	 public static final String APP_ID = "wxd06ed21aee3cbe90";

    // // 潮博会(android-test)
    // public static final String APP_ID = "wx10a2ce3e30ba6604";

	private static final int IMG_THUMB_SIZE = 150;

	private Context mContext;

	private IWXAPI api;

	public WeiXin(Context context) {
		mContext = context;
		api = WXAPIFactory.createWXAPI(mContext, APP_ID);
		api.registerApp(APP_ID);
		api.handleIntent(null, this);
	}

	public boolean isWXAppSupportAPI() {
		return api.isWXAppSupportAPI();
	}

	public boolean isWXAppInstalled() {
		return api.isWXAppInstalled();
	}

	public void upload(String text, boolean scence) {
		if (text == null || text.length() == 0) {
			return;
		}

		// 初始化一个WXTextObject对象
		WXTextObject textObj = new WXTextObject();
		textObj.text = text;

		// 用WXTextObject对象初始化一个WXMediaMessage对象
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = textObj;
		// 发送文本类型的消息时，title字段不起作用
		// msg.title = "Will be ignored";
		msg.description = text;

		// 构造一个Req
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求
		req.message = msg;
		req.scene = scence ? SendMessageToWX.Req.WXSceneTimeline
				: SendMessageToWX.Req.WXSceneSession;

		// 调用api接口发送数据到微信
		api.sendReq(req);
	}

	public void upload(final String text, final String imageUrl) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					WXImageObject imgObj = new WXImageObject();
					imgObj.imageUrl = imageUrl;

					WXMediaMessage msg = new WXMediaMessage();
					msg.mediaObject = imgObj;

					msg.description = text;
					msg.title = text;

					Bitmap bmp = BitmapFactory.decodeStream(new URL(imageUrl)
							.openStream());
					Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,
							IMG_THUMB_SIZE, IMG_THUMB_SIZE, true);
					bmp.recycle();
					msg.thumbData = Utils.bmpToByteArray(thumbBmp, true);

					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("img");
					req.message = msg;
					req.scene = SendMessageToWX.Req.WXSceneTimeline;
					api.sendReq(req);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	@Override
	public void onReq(BaseReq arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResp(BaseResp resp) {
		String result = "";

		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = "共享成功";
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = "发送取消";
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = "发送被拒绝";
			break;
		default:
			result = "发送返回";
			break;
		}

		Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();

	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

}
